/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Design.java
 *
 * Created on Oct 16, 2012, 8:25:51 PM
 */
package UserInterface;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import controller.ColorMeanController;
import controller.SkinBasedController;
import insurface.InteractiveSurface;
import java.awt.BasicStroke;
import java.awt.Desktop;
import java.awt.Graphics2D;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import utilities.CalibrateTouch;
import utilities.CoordinateFinder;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author HOME
 */
public class ProjectorGUI extends InteractiveSurface {

    /** Creates new form Design */
    private static int saveImageNum = 0;
    private ImageIcon ic1 = new ImageIcon();
    private ImageIcon ic2 = new ImageIcon();
    private ImageIcon ic3 = new ImageIcon();
    private ImageIcon ic4 = new ImageIcon();
    private ImageIcon drawing = new ImageIcon();
    static boolean isImageSet = false;
    private CvSize imSize = new CvSize(217, 187);
    private IplImage tempIm = cvCreateImage(imSize, 8, 3);
    private IplImage tempSkin = cvCreateImage(imSize, 8, 1);
    private IplImage tempIm2 = cvCreateImage(imSize, 8, 3);
    private IplImage tempSkin2 = cvCreateImage(imSize, 8, 1);
    private static int ii = 0;
    private int prevX = 0, prevY = 0;
    static Timer initTimer;
    Timer timer;
    Thread thread;
    Color linecolor = Color.BLACK;
    ButtonGroup colorButton = new ButtonGroup();
    private boolean viewCamPressed = false;
    private boolean deviceStarted = false, clearSet = false, isFingerSet = true;
    Graphics2D g;
    JFileChooser fc = new JFileChooser();
    File file;
    BufferedImage bf, saveImage, newImage;
    BufferedImage[] imageDb = new BufferedImage[5];
    BufferedImage[] iconDb = new BufferedImage[5];
    Robot rb;
    boolean isCalibrating = false;
    boolean isCal1, isCal2, isCal3;
    boolean finished1, finished2, finished3;
    int minX = 4000, maxX = 0, count = 0;
    int totX, totY, minY = 1000, maxY = 0;
    int x1, y1, x2, y2, x3, y3;
    double a = 1, b, c, d, e = 1, f;//used for touchpad calibration process
    int state = 0;
    int[] xVals = new int[15], yVals = new int[15];

    private boolean inRange1(int x, int y) {
        if (250 < x && x < 550 && 0 < y && y < 400) {
            return true;
        } else {
            return false;
        }
    }

    private boolean inRange2(int x, int y) {
        if (0 < x && x < 200 && 500 < y && y < 1200) {
            return true;
        } else {
            return false;
        }
    }

    private boolean inRange3(int x, int y) {
        if (550 < x && x < 1000 && 500 < y && y < 1200) {
            return true;
        } else {
            return false;
        }
    }

    public ProjectorGUI() {
        initComponents();
        colorButton.add(blackRadioButton);
        colorButton.add(blueRadioButton);
        colorButton.add(greenRadioButton);
        colorButton.add(redRadioButton);
        blackRadioButton.setSelected(true);
        g = (Graphics2D) drawingPanel.getGraphics();
   
        try {
            rb = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (int i = 0; i < iconDb.length; i++) {
            try {
                String iconName = "src/images/" + (i + 1) + ".png";
                iconDb[i] = ImageIO.read(new File(iconName));

            } catch (IOException ex) {
                Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        drawingPanel = new javax.swing.JPanel();
        drawLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        leftPanel = new javax.swing.JPanel();
        leftLivecamPanel = new javax.swing.JPanel();
        leftLiveCamLabel = new javax.swing.JLabel();
        leftSkinPanel = new javax.swing.JPanel();
        leftSkinLabel = new javax.swing.JLabel();
        rightPanel = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        rightSkinLabel = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        rightLiveCamLabel = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jSlider1 = new javax.swing.JSlider(1,10,5);
        jPanel6 = new javax.swing.JPanel();
        blackRadioButton = new javax.swing.JRadioButton();
        redRadioButton = new javax.swing.JRadioButton();
        greenRadioButton = new javax.swing.JRadioButton();
        blueRadioButton = new javax.swing.JRadioButton();
        jToolBar1 = new javax.swing.JToolBar();
        startButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        cameraButton = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        blackPenButton = new javax.swing.JButton();
        redPenButton = new javax.swing.JButton();
        bluePenButton = new javax.swing.JButton();
        eraserButton = new javax.swing.JButton();
        clearScreen = new javax.swing.JButton();
        imageButton = new javax.swing.JButton();
        jToolBar3 = new javax.swing.JToolBar();
        nextButton = new javax.swing.JButton();
        prevButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        deviceMenu1 = new javax.swing.JMenuItem();
        stopMenu = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        fingerMenu = new javax.swing.JMenuItem();
        redStylusMenu = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        leftCamMenu = new javax.swing.JMenuItem();
        rightCamMenu = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        powerPointMenu = new javax.swing.JMenuItem();
        photoViewerMenu = new javax.swing.JMenuItem();
        toolMenu = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        saveMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Interactive Surface");
        setAlwaysOnTop(true);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        drawingPanel.setBackground(new java.awt.Color(255, 255, 255));
        drawingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Screen", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        drawingPanel.setNextFocusableComponent(drawingPanel);
        drawingPanel.setOpaque(false);
        drawingPanel.setPreferredSize(new java.awt.Dimension(800, 600));

        javax.swing.GroupLayout drawingPanelLayout = new javax.swing.GroupLayout(drawingPanel);
        drawingPanel.setLayout(drawingPanelLayout);
        drawingPanelLayout.setHorizontalGroup(
            drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(drawLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 807, Short.MAX_VALUE)
        );
        drawingPanelLayout.setVerticalGroup(
            drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(drawLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Display", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        leftPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Left Camera", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        leftLivecamPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Live Camera", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        leftLivecamPanel.setPreferredSize(new java.awt.Dimension(210, 210));

        javax.swing.GroupLayout leftLivecamPanelLayout = new javax.swing.GroupLayout(leftLivecamPanel);
        leftLivecamPanel.setLayout(leftLivecamPanelLayout);
        leftLivecamPanelLayout.setHorizontalGroup(
            leftLivecamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftLivecamPanelLayout.createSequentialGroup()
                .addComponent(leftLiveCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                .addContainerGap())
        );
        leftLivecamPanelLayout.setVerticalGroup(
            leftLivecamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(leftLiveCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
        );

        leftSkinPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Skin Image", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        leftSkinPanel.setPreferredSize(new java.awt.Dimension(210, 210));

        javax.swing.GroupLayout leftSkinPanelLayout = new javax.swing.GroupLayout(leftSkinPanel);
        leftSkinPanel.setLayout(leftSkinPanelLayout);
        leftSkinPanelLayout.setHorizontalGroup(
            leftSkinPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftSkinPanelLayout.createSequentialGroup()
                .addComponent(leftSkinLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addContainerGap())
        );
        leftSkinPanelLayout.setVerticalGroup(
            leftSkinPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(leftSkinLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addComponent(leftLivecamPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(leftSkinPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftPanelLayout.createSequentialGroup()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(leftSkinPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                    .addComponent(leftLivecamPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
                .addContainerGap())
        );

        rightPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Right Camera", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Skin Image", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        jPanel9.setPreferredSize(new java.awt.Dimension(210, 210));

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addComponent(rightSkinLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(rightSkinLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Live Camera", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        jPanel7.setPreferredSize(new java.awt.Dimension(210, 210));

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(rightLiveCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(rightLiveCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout rightPanelLayout = new javax.swing.GroupLayout(rightPanel);
        rightPanel.setLayout(rightPanelLayout);
        rightPanelLayout.setHorizontalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
        );
        rightPanelLayout.setVerticalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
            .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(rightPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(leftPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(leftPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rightPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Pen", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Line Width", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        jPanel5.setFont(new java.awt.Font("Tahoma", 0, 12));

        jSlider1.setForeground(new java.awt.Color(0, 0, 0));
        jSlider1.setMajorTickSpacing(1);
        jSlider1.setMaximum(10);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setPaintTrack(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setValue(5);
        jSlider1.setAutoscrolls(true);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Color", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        blackRadioButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        blackRadioButton.setText("Black");
        blackRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blackRadioButtonActionPerformed(evt);
            }
        });

        redRadioButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        redRadioButton.setText("Red");
        redRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redRadioButtonActionPerformed(evt);
            }
        });

        greenRadioButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        greenRadioButton.setText("Green");
        greenRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                greenRadioButtonActionPerformed(evt);
            }
        });

        blueRadioButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        blueRadioButton.setText("Blue");
        blueRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blueRadioButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(blackRadioButton)
                    .addComponent(blueRadioButton))
                .addGap(36, 36, 36)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(greenRadioButton)
                    .addComponent(redRadioButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blackRadioButton)
                    .addComponent(greenRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blueRadioButton)
                    .addComponent(redRadioButton)))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);
        jToolBar1.setAutoscrolls(true);

        startButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Play.png"))); // NOI18N
        startButton.setToolTipText("Start Device");
        startButton.setFocusable(false);
        startButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        startButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(startButton);

        pauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/player-pause.png"))); // NOI18N
        pauseButton.setFocusable(false);
        pauseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        pauseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        pauseButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                pauseButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                pauseButtonMouseExited(evt);
            }
        });
        pauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pauseButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(pauseButton);

        stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Stop playing.png"))); // NOI18N
        stopButton.setToolTipText("Stop Device");
        stopButton.setFocusable(false);
        stopButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        stopButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(stopButton);

        cameraButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Camera.png"))); // NOI18N
        cameraButton.setToolTipText("Show Cameras");
        cameraButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cameraButton.setOpaque(false);
        cameraButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cameraButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cameraButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                cameraButtonMouseExited(evt);
            }
        });
        cameraButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cameraButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(cameraButton);

        jToolBar2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar2.setRollover(true);

        blackPenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Pen-icon.png"))); // NOI18N
        blackPenButton.setToolTipText("Pen");
        blackPenButton.setFocusable(false);
        blackPenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        blackPenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        blackPenButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                blackPenButtonMouseEntered(evt);
            }
        });
        blackPenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blackPenButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(blackPenButton);

        redPenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Pen-Red-icon.png"))); // NOI18N
        redPenButton.setFocusable(false);
        redPenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        redPenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        redPenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redPenButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(redPenButton);

        bluePenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Pen-Blue-icon.png"))); // NOI18N
        bluePenButton.setFocusable(false);
        bluePenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bluePenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bluePenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bluePenButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(bluePenButton);

        eraserButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Eraser.png"))); // NOI18N
        eraserButton.setToolTipText("Eraser");
        eraserButton.setFocusable(false);
        eraserButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        eraserButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        eraserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eraserButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(eraserButton);

        clearScreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Clear.png"))); // NOI18N
        clearScreen.setToolTipText("Clear Screen");
        clearScreen.setFocusable(false);
        clearScreen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        clearScreen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        clearScreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearScreenActionPerformed(evt);
            }
        });
        jToolBar2.add(clearScreen);

        imageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/New image.png"))); // NOI18N
        imageButton.setToolTipText("Insert Image");
        imageButton.setFocusable(false);
        imageButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        imageButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        imageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imageButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(imageButton);

        jToolBar3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar3.setRollover(true);

        nextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Forward.png"))); // NOI18N
        nextButton.setFocusable(false);
        nextButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        nextButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        jToolBar3.add(nextButton);

        prevButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Back.png"))); // NOI18N
        prevButton.setFocusable(false);
        prevButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        prevButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        prevButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevButtonActionPerformed(evt);
            }
        });
        jToolBar3.add(prevButton);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/1.png"))); // NOI18N
        jButton1.setToolTipText("Page 1");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar3.add(jButton1);

        jMenu1.setText("Device");
        jMenu1.setFont(new java.awt.Font("Arial", 1, 12));

        deviceMenu1.setFont(new java.awt.Font("Arial", 1, 12));
        deviceMenu1.setText("Start Device");
        deviceMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deviceMenu1ActionPerformed(evt);
            }
        });
        jMenu1.add(deviceMenu1);

        stopMenu.setFont(new java.awt.Font("Arial", 1, 12));
        stopMenu.setText("Stop Device");
        stopMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopMenuActionPerformed(evt);
            }
        });
        jMenu1.add(stopMenu);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Stylus");
        jMenu2.setFont(new java.awt.Font("Arial", 1, 12));

        fingerMenu.setFont(new java.awt.Font("Arial", 1, 12));
        fingerMenu.setText("Finger");
        fingerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fingerMenuActionPerformed(evt);
            }
        });
        jMenu2.add(fingerMenu);

        redStylusMenu.setFont(new java.awt.Font("Arial", 1, 12));
        redStylusMenu.setText("Red Stylus");
        redStylusMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redStylusMenuActionPerformed(evt);
            }
        });
        jMenu2.add(redStylusMenu);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Display");
        jMenu3.setFont(new java.awt.Font("Arial", 1, 12));

        leftCamMenu.setFont(new java.awt.Font("Arial", 1, 12));
        leftCamMenu.setText("Start Left Camera");
        leftCamMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leftCamMenuActionPerformed(evt);
            }
        });
        jMenu3.add(leftCamMenu);

        rightCamMenu.setFont(new java.awt.Font("Arial", 1, 12));
        rightCamMenu.setText("Start Right Camera");
        rightCamMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightCamMenuActionPerformed(evt);
            }
        });
        jMenu3.add(rightCamMenu);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Application");
        jMenu4.setFont(new java.awt.Font("Arial", 1, 12));

        jMenuItem2.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem2.setText("Free Hand");
        jMenu4.add(jMenuItem2);

        powerPointMenu.setFont(new java.awt.Font("Arial", 1, 12));
        powerPointMenu.setText("Power Point");
        powerPointMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                powerPointMenuActionPerformed(evt);
            }
        });
        jMenu4.add(powerPointMenu);

        photoViewerMenu.setFont(new java.awt.Font("Arial", 1, 12));
        photoViewerMenu.setText("Photo Viewer");
        photoViewerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                photoViewerMenuActionPerformed(evt);
            }
        });
        jMenu4.add(photoViewerMenu);

        jMenuBar1.add(jMenu4);

        toolMenu.setText("Tools");
        toolMenu.setFont(new java.awt.Font("Arial", 1, 12));

        jMenuItem9.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem9.setText("Eraser");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        toolMenu.add(jMenuItem9);

        jMenuItem10.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem10.setText("Clear Screen");
        toolMenu.add(jMenuItem10);

        saveMenu.setFont(new java.awt.Font("Arial", 1, 12));
        saveMenu.setText("Save Images");
        saveMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuActionPerformed(evt);
            }
        });
        toolMenu.add(saveMenu);

        jMenuBar1.add(toolMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 817, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar3, 0, 0, Short.MAX_VALUE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(drawingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46)
                        .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public BufferedImage getLiveCam1(int num) {

        if (num == 0) {
            cvResize(imgArr[num][0], tempIm);
            return tempIm.getBufferedImage();
        } else {
            cvResize(imgArr[num][0], tempIm2);
            return tempIm2.getBufferedImage();
        }
    }

    public BufferedImage getSkin1(int num) {
        if (num == 0) {
            cvResize(imgArr[num][1], tempSkin);
            return tempSkin.getBufferedImage();
        } else {
            cvResize(imgArr[num][1], tempSkin2);
            return tempSkin2.getBufferedImage();
        }
    }

    private void blackRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_blackRadioButtonActionPerformed
        linecolor = Color.BLACK;
}//GEN-LAST:event_blackRadioButtonActionPerformed

    private void deviceMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deviceMenu1ActionPerformed
        if (deviceStarted) {
            timer.restart();
        } else {
            deviceStarted = true;
            startButton.setEnabled(false);
            stopButton.setEnabled(true);

            saveImage = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
            for (int i = 0; i < imageDb.length; i++) {
                imageDb[i] = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
            }
            g = (Graphics2D) saveImage.getGraphics();
            if (isFingerSet) {

                control = new SkinBasedController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            } else {
                control = new ColorMeanController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            }

            thread.start();
            initTimer = new Timer(50, initialTimer);
            initTimer.start();
        }
    }//GEN-LAST:event_deviceMenu1ActionPerformed

    private void leftCamMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leftCamMenuActionPerformed
        viewCamPressed = true;
    }//GEN-LAST:event_leftCamMenuActionPerformed

    private void stopMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopMenuActionPerformed
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        deviceMenu1.setEnabled(true);
        stopMenu.setEnabled(false);
        thread.stop();
    }//GEN-LAST:event_stopMenuActionPerformed

    private void greenRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_greenRadioButtonActionPerformed
        linecolor = Color.GREEN;
    }//GEN-LAST:event_greenRadioButtonActionPerformed

    private void blueRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_blueRadioButtonActionPerformed
        linecolor = Color.BLUE;
    }//GEN-LAST:event_blueRadioButtonActionPerformed

    private void redRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redRadioButtonActionPerformed
        linecolor = Color.RED;
    }//GEN-LAST:event_redRadioButtonActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        linecolor = Color.WHITE;
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void rightCamMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightCamMenuActionPerformed
        viewCamPressed = true;
    }//GEN-LAST:event_rightCamMenuActionPerformed

    private void cameraButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cameraButtonActionPerformed
        if (!deviceStarted) {
            JOptionPane.showMessageDialog(drawingPanel, "Please Start The Device First.", "Information", 1);
        } else if (!setCamImage) {
            JOptionPane.showMessageDialog(drawingPanel, "Please wait until cameras are loaded.", "Information", 1);
        } else {
            viewCamPressed = true;
        }
    }//GEN-LAST:event_cameraButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        if (deviceStarted) {
            pauseButton.setEnabled(true);
            timer.restart();
        } else {
            isCalibrating = true;
            deviceStarted = true;
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            saveImage = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
            for (int i = 0; i < imageDb.length; i++) {
                imageDb[i] = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
            }
            g = (Graphics2D) saveImage.getGraphics();
            if (isFingerSet) {
                System.out.println("left=" + left_Camera + "Right=" + right_Camera);
                control = new SkinBasedController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            } else {
                control = new ColorMeanController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            }

            thread.start();
            timer = new Timer(15, paintTimer);
            timer.start();
//            initTimer = new Timer(50, initialTimer);
//            initTimer.start();
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed

        if (!deviceStarted) {
            JOptionPane.showMessageDialog(drawingPanel, "Please Start The Device First.", "Information", 2);
        } else {
            stopButton.setEnabled(false);
            stopMenu.setEnabled(false);
            int confirm = JOptionPane.showConfirmDialog(drawingPanel, "Do You Really Want To Stop The Device?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);

            if (confirm == 0) {
                viewCamPressed = false;
                control.setActive(false);
                deviceStarted = false;
                startButton.setEnabled(true);
                imgArr = new IplImage[2][2];
                leftLiveCamLabel.setIcon(null);
                leftSkinLabel.setIcon(null);
                rightLiveCamLabel.setIcon(null);
                rightSkinLabel.setIcon(null);
            }
        }
    }//GEN-LAST:event_stopButtonActionPerformed

    private void eraserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eraserButtonActionPerformed
        linecolor = getBackground();
    }//GEN-LAST:event_eraserButtonActionPerformed

    private void blackPenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_blackPenButtonActionPerformed
        linecolor = Color.BLACK;
    }//GEN-LAST:event_blackPenButtonActionPerformed

    private void clearScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearScreenActionPerformed
        saveImage = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    }//GEN-LAST:event_clearScreenActionPerformed

    private void imageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imageButtonActionPerformed
        setBackgroundImage();
    }//GEN-LAST:event_imageButtonActionPerformed

    private void powerPointMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_powerPointMenuActionPerformed

        Desktop dt = Desktop.getDesktop();
        JFileChooser fchose = new JFileChooser();
        fchose.showOpenDialog(this);
        File f = fchose.getSelectedFile();
        try {
            dt.open(f);
        } catch (IOException ex) {
            System.out.println("Power point openning error " + ex);
        }
    }//GEN-LAST:event_powerPointMenuActionPerformed

    private void photoViewerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_photoViewerMenuActionPerformed

        Desktop dt = Desktop.getDesktop();
        JFileChooser fchose = new JFileChooser();
        fchose.showOpenDialog(this);
        File f = fchose.getSelectedFile();
        try {
            dt.open(f);
        } catch (IOException ex) {
            //Logger.getLogger(Design1.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Photo Viewer openning error " + ex);
        }
    }//GEN-LAST:event_photoViewerMenuActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        imageDb[saveImageNum] = saveImage;
        //System.out.println(saveImageNum);
        setImageLabel(saveImageNum, +1);
        if (saveImageNum == 4) {
            saveImageNum = -1;
        }
        saveImageNum++;
        clearSet = true;
    }//GEN-LAST:event_nextButtonActionPerformed

    private void prevButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevButtonActionPerformed
        imageDb[saveImageNum] = saveImage;
        setImageLabel(saveImageNum, -1);
        if (saveImageNum == 0) {
            saveImageNum = 5;
        }
        saveImageNum--;
        clearSet = true;
    }//GEN-LAST:event_prevButtonActionPerformed

    private void redStylusMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redStylusMenuActionPerformed
        isFingerSet = false;
    }//GEN-LAST:event_redStylusMenuActionPerformed

    private void fingerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fingerMenuActionPerformed
        isFingerSet = true;
    }//GEN-LAST:event_fingerMenuActionPerformed

    private void saveMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuActionPerformed
        File fiile = null;
        int selection = JOptionPane.showConfirmDialog(drawingPanel, "Do you really want to save the images?", "Save", 1);
        if (selection == 0) {
            JFileChooser ffc = new JFileChooser();
            ffc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = ffc.showDialog(this, "Save");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                fiile = ffc.getSelectedFile();
            }
            imageDb[saveImageNum] = saveImage;
            for (int i = 0; i < imageDb.length; i++) {
                try {
                    String fileNum = fiile.getAbsolutePath() + "\\Drawing_" + (i + 1) + ".jpg";
                    ImageIO.write(imageDb[i], "jpg", new java.io.File(fileNum));
                } catch (java.io.IOException e) {
                    System.out.println("Unable to save image:\n" + e);
                }
            }
        }
    }//GEN-LAST:event_saveMenuActionPerformed

    private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
        timer.stop();
        pauseButton.setEnabled(false);
        startButton.setEnabled(true);
    }//GEN-LAST:event_pauseButtonActionPerformed
    public static boolean mouseInPause = false;
    private void pauseButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseButtonMouseEntered
        // TODO add your handling code here:

        mouseInPause = true;
        if (mouseInPause) {
//            try {
//                for (int i = 0; i < 100000; i++) {
//                   // System.out.println(i);
//                }
//                rb = new Robot();
//                rb.mousePress(MouseEvent.BUTTON1_MASK);
//                rb.mouseRelease((MouseEvent.BUTTON1_MASK));
//                mouseInPause = false;
//            } catch (AWTException ex) {
//                Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
//            }
        }
    }//GEN-LAST:event_pauseButtonMouseEntered

    private void pauseButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseButtonMouseExited


        mouseInPause = false;
    }//GEN-LAST:event_pauseButtonMouseExited
    public static boolean mouseInCamera = false;
    private void cameraButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cameraButtonMouseEntered
        // TODO add your handling code here:

        mouseInCamera = true;

//        try {
//            for (int i = 0; i < 100000; i++) {
//                //System.out.println(i);
//                if (!mouseInCamera) {
//                    break;
//                }
//                if (i == 99999) {
//                    rb = new Robot();
//                    rb.mousePress(MouseEvent.BUTTON1_MASK);
//                    rb.mouseRelease((MouseEvent.BUTTON1_MASK));
//                }
//            }
//
//        } catch (AWTException ex) {
//            Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
//        }


    }//GEN-LAST:event_cameraButtonMouseEntered

    private void cameraButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cameraButtonMouseExited
        // TODO add your handling code here:

        mouseInCamera = false;
    }//GEN-LAST:event_cameraButtonMouseExited

    private void blackPenButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_blackPenButtonMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_blackPenButtonMouseEntered

    private void redPenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redPenButtonActionPerformed
        linecolor = Color.RED;
    }//GEN-LAST:event_redPenButtonActionPerformed

    private void bluePenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bluePenButtonActionPerformed
        linecolor = Color.BLUE;
    }//GEN-LAST:event_bluePenButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

                } catch (Exception e) {
                    System.out.println("Look and Feel doesn't available!!!");
                }
                new ProjectorGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton blackPenButton;
    private javax.swing.JRadioButton blackRadioButton;
    private javax.swing.JButton bluePenButton;
    private javax.swing.JRadioButton blueRadioButton;
    private javax.swing.JButton cameraButton;
    private javax.swing.JButton clearScreen;
    private javax.swing.JMenuItem deviceMenu1;
    private javax.swing.JLabel drawLabel;
    private javax.swing.JPanel drawingPanel;
    private javax.swing.JButton eraserButton;
    private javax.swing.JMenuItem fingerMenu;
    private javax.swing.JRadioButton greenRadioButton;
    private javax.swing.JButton imageButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JMenuItem leftCamMenu;
    private javax.swing.JLabel leftLiveCamLabel;
    private javax.swing.JPanel leftLivecamPanel;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JLabel leftSkinLabel;
    private javax.swing.JPanel leftSkinPanel;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton pauseButton;
    private javax.swing.JMenuItem photoViewerMenu;
    private javax.swing.JMenuItem powerPointMenu;
    private javax.swing.JButton prevButton;
    private javax.swing.JButton redPenButton;
    private javax.swing.JRadioButton redRadioButton;
    private javax.swing.JMenuItem redStylusMenu;
    private javax.swing.JMenuItem rightCamMenu;
    private javax.swing.JLabel rightLiveCamLabel;
    private javax.swing.JPanel rightPanel;
    private javax.swing.JLabel rightSkinLabel;
    private javax.swing.JMenuItem saveMenu;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JMenuItem stopMenu;
    private javax.swing.JMenu toolMenu;
    // End of variables declaration//GEN-END:variables
    Action paintTimerNew = new AbstractAction() {

        public void actionPerformed(ActionEvent event) {
            int srcLength = CoordinateFinder.getLENGTH();
            int valX = srcLength - getX1();
            int valY = getY1();
            //    System.out.println("x= "+valX+" y= "+valY);
            g = (Graphics2D) saveImage.getGraphics();
            g.setBackground(drawingPanel.getBackground());

            if (viewCamPressed) {
                liveCamViewer();
            }
            if (isCalibrating) {
                if (isCal1 && !finished1) {
                    g.setColor(Color.RED);
                    g.fillOval(400, 200, 10, 10);
                    count++;
                    if (valX < minX) {
                        minX = valX;
                    }
                    if (valX > maxX) {
                        maxX = valX;
                    }
                    if (valY < minY) {
                        minY = valY;
                    }
                    if (valY > maxY) {
                        maxY = valY;
                    }
                    totX += valX;
                    totY += valY;
                    if (maxX - minX > 50 || maxY - minY > 50) {
                        g.setColor(Color.BLACK);
                        g.fillOval(400, 200, 10, 10);
                        minX = 4000;
                        maxX = 0;
                        count = 0;
                        totX = 0;
                        totY = 0;
                        minY = 1000;
                        maxY = 0;
                    } else if (count == 15) {
                        finished1 = true;
                        g.setColor(Color.GREEN);
                        g.fillOval(400, 200, 10, 10);
                        x1 = totX / 15;
                        y1 = totY / 15;
                        minX = 4000;
                        maxX = 0;
                        count = 0;
                        totX = 0;
                        totY = 0;
                        minY = 1000;
                        maxY = 0;
                    }

                } else if (isCal2 && !finished2) {
                    isCal2 = inRange2(valX, valY);
                    if (!isCal2) {
                        //going back to the initial state
                        minX = 4000;
                        maxX = 0;
                        count = 0;
                        totX = 0;
                        totY = 0;
                        minY = 1000;
                        maxY = 0;
                    } else {
                        g.setColor(Color.RED);
                        g.fillOval(100, 550, 10, 10);
                        count++;
                        if (valX < minX) {
                            minX = valX;
                        }
                        if (valX > maxX) {
                            maxX = valX;
                        }
                        if (valY < minY) {
                            minY = valY;
                        }
                        if (valY > maxY) {
                            maxY = valY;
                        }
                        totX += valX;
                        totY += valY;
                        if (maxX - minX > 50 || maxY - minY > 50) {
                            g.setColor(Color.BLACK);
                            g.fillOval(100, 550, 10, 10);
                            minX = 4000;
                            maxX = 0;
                            count = 0;
                            totX = 0;
                            totY = 0;
                            minY = 1000;
                            maxY = 0;
                        } else if (count == 15) {
                            finished1 = true;
                            g.setColor(Color.GREEN);
                            g.fillOval(100, 550, 10, 10);
                            x2 = totX / 15;
                            y2 = totY / 15;
                            minX = 4000;
                            maxX = 0;
                            count = 0;
                            totX = 0;
                            totY = 0;
                            minY = 1000;
                            maxY = 0;
                        }
                    }
                } else if (isCal3 && !finished3) {
                    isCal1 = inRange1(valX, valY);
                    if (!isCal1) {
                        //going back to the initial state
                        minX = 4000;
                        maxX = 0;
                        count = 0;
                        totX = 0;
                        totY = 0;
                        minY = 1000;
                        maxY = 0;
                    } else {
                        g.setColor(Color.RED);
                        g.fillOval(700, 550, 10, 10);
                        count++;
                        if (valX < minX) {
                            minX = valX;
                        }
                        if (valX > maxX) {
                            maxX = valX;
                        }
                        if (valY < minY) {
                            minY = valY;
                        }
                        if (valY > maxY) {
                            maxY = valY;
                        }
                        totX += valX;
                        totY += valY;
                        if (maxX - minX > 50 || maxY - minY > 50) {
                            g.setColor(Color.BLACK);
                            g.fillOval(700, 550, 10, 10);
                            minX = 4000;
                            maxX = 0;
                            count = 0;
                            totX = 0;
                            totY = 0;
                            minY = 1000;
                            maxY = 0;
                        } else if (count == 15) {
                            finished1 = true;
                            g.setColor(Color.GREEN);
                            g.fillOval(700, 550, 10, 10);
                            x1 = totX / 15;
                            y1 = totY / 15;
                            minX = 4000;
                            maxX = 0;
                            count = 0;
                            totX = 0;
                            totY = 0;
                            minY = 1000;
                            maxY = 0;
                        }
                    }
                } else if (finished1 && finished2 && finished3) {
                    CalibrateTouch ct = new CalibrateTouch();
                    double[] rt = ct.getMatrix(x1, y1, x2, y2, x3, y3, 400, 100, 100, 550, 700, 550);
                    a = rt[0];
                    b = rt[1];
                    c = rt[2];
                    d = rt[3];
                    e = rt[4];
                    f = rt[5];
                    isCalibrating = false;
                    saveImage = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
                    //get the calobrartion matrix
                } else {
                    if (!finished1 && !isCal1) {
                        g.setColor(linecolor);
                        g.fillOval(400, 200, 10, 10);

                    }


                    int xx = srcLength - getX1();
                    //       int yy = getY() - drawLabel.getLocationOnScreen().y;
                    int yy = getY1();

                    if (!finished2 && !isCal2) {
                        g.setColor(linecolor);
                        g.fillOval(100, 550, 10, 10);

                    }

                    if (!finished3 && !isCal3) {
                        g.setColor(linecolor);
                        g.fillOval(700, 550, 10, 10);

                    }
                    isCal1 = inRange1(valX, valY);
                    isCal2 = inRange2(valX, valY);
                    isCal3 = inRange3(valX, valY);
                }
                if (finished1) {
                    g.setColor(Color.GREEN);
                    g.fillOval(400, 200, 10, 10);
                }
                if (finished2) {
                    g.setColor(Color.GREEN);
                    g.fillOval(100, 550, 10, 10);
                }
                if (finished3) {
                    g.setColor(Color.GREEN);
                    g.fillOval(700, 550, 10, 10);
                }
                drawing.setImage(saveImage);
                drawLabel.setIcon(drawing);
                drawingPanel.repaint();

            } else if (!clearSet) {

                int xx = srcLength - getX1();
                int yy = getY1() - drawLabel.getLocationOnScreen().y;

                xx = (int) (a * xx + b * yy + c);
                yy = (int) (d * xx + e * yy + f);
                //yy=yy-100;
                //System.out.println("Label-"+drawLabel.getX()+","+drawLabel.getY()+"\n JPanel="+drawingPanel.getX()+","+drawingPanel.getY());
                //System.out.println("Label-"+drawLabel.getLocationOnScreen().x+","+drawLabel.getLocationOnScreen().y+"\n JPanel="+drawingPanel.getLocationOnScreen().x+","+drawingPanel.getLocationOnScreen().y);
                if ((prevX == 0 && prevY == 0) || (xx == 0 && yy == 0)) {
                    try {
                        Robot rb = new Robot();
                    } catch (AWTException ex) {
                        Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    g.setColor(linecolor);
                    g.setStroke(new BasicStroke(jSlider1.getValue()));
                    //System.out.println("prevX= " + prevX + " prevY= " + prevY + " x= " + xx + " y= " + yy);
                    //  System.out.println("x= " + xx + " y= " + yy);
                    //     if (xx < 800) {
                    g.fillOval(xx, yy, 10, 10);
                    //g.drawLine(prevX, prevY, xx, yy);
                    drawing.setImage(saveImage);
                    drawLabel.setIcon(drawing);
                    drawingPanel.repaint();
                    //     } else {
                        /*
                    rb.mouseMove(xx, yy);
                    System.out.println("mouse moving");*/
                    //    }

                }
                prevX = xx;
                prevY = yy;

            } else {
                saveImage = imageDb[saveImageNum];
                clearSet = false;

            }
        }
    };

    public void liveCamViewer() {

        //System.out.println("dgtyuii");
        ic1.setImage(getLiveCam1(0));
        //System.out.println("llllllll");
        leftLiveCamLabel.setIcon(ic1);

        //System.out.println("dgtyuii3333333");
        ic2.setImage(getSkin1(0));
        //System.out.println("dgtyuii6666666");
        leftSkinLabel.setIcon(ic2);

        ic3.setImage(getLiveCam1(1));
        //System.out.println("dgtyuii99999999");
        rightLiveCamLabel.setIcon(ic3);
        //System.out.println("dgtyuii22222222");

        ic4.setImage(getSkin1(1));
        //System.out.println("dgtyuii444444444");

        rightSkinLabel.setIcon(ic4);

        leftPanel.repaint();
        rightPanel.repaint();
    }

    public void setBackgroundImage() {
        if (fc == null) {
            fc = new JFileChooser();

            //     fc.addChoosableFileFilter(new ImageFilter());
            fc.setAcceptAllFileFilterUsed(false);
            fc.setFileFilter(new ImageFilter());
            fc.setAccessory(new ImagePreview(fc));
        }
        int returnVal = fc.showDialog(this, "Open");

        //Process the results.
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();

        } else {
        }

        fc.setSelectedFile(null);
        try {
            bf = ImageIO.read(file);
        } catch (IOException ex) {
            Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        saveImage = bf;

    }

    public void setImageLabel(int num, int sign) {
        switch (num + 1) {
            case 1:
                // jButton1.setEnabled(false);

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[1]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[4]));
                    jButton1.repaint();
                }
                break;
            case 2:

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[2]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[0]));
                    jButton1.repaint();
                }
                break;
            case 3:

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[3]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[1]));
                    jButton1.repaint();
                }
                break;
            case 4:

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[4]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[2]));
                    jButton1.repaint();
                }
                break;
            case 5: //  jButton5.setEnabled(false);
                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[0]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[3]));
                    jButton1.repaint();
                }
                break;
        }
    }
    Action initialTimer = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            try {
                BufferedImage ani = null;
                BufferedImage subAni = null;

                ani = ImageIO.read(new File((getClass().getResource("/UserInterface/board.jpg")).getPath()));

                int inity = ani.getHeight() / 10;

                subAni = ani.getSubimage(0, 0, ani.getWidth(), (ii + 1) * inity);

                drawLabel.setIcon(new javax.swing.ImageIcon(subAni));
                drawingPanel.repaint();
                Thread.sleep(500);
                if (ii == 9) {
                    Thread.sleep(1000);
                    initTimer.stop();
                    timer = new Timer(15, paintTimer);
                    timer.start();
                }
                ii++;
                ani.flush();
                subAni.flush();

            } catch (InterruptedException ex) {
                Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    };
    Action paintTimer = new AbstractAction() {

        public void actionPerformed(ActionEvent event) {
            int srcLength = CoordinateFinder.getLENGTH();
            int valX = srcLength - getX1();
            int valY = getY1();
                System.out.println("x= "+valX+" y= "+valY);
            g = (Graphics2D) saveImage.getGraphics();
            g.setBackground(drawingPanel.getBackground());

            if (viewCamPressed) {
                liveCamViewer();
            }
            if (isCalibrating) {
                isCal1 = inRange1(valX, valY);
                isCal2 = inRange2(valX, valY);
                isCal3 = inRange3(valX, valY);
                switch (state) {
                    case 0:
                        if (finished1 && finished2 && finished3) {
                            state = 4;
                        } else if (isCal1 && !finished1) {
                            count = 0;
                            state = 1;
                        } else if (isCal2 && !finished2) {
                            count = 0;
                            state = 2;
                        } else if (isCal3 && !finished3) {
                            count = 0;
                            state = 3;
                        }
                        break;
                    case 1:
                        if (!isCal1) {

                            state = 0;
                            xVals = new int[15];
                            yVals = new int[15];
                            count = 0;
                        } else if (count == 15) {

                            finished1 = true;
                            x1 = (int) getAverage(xVals);
                            y1 = (int) getAverage(yVals);
                            xVals = new int[15];
                            yVals = new int[15];
                            count = 0;
                        } else {
                            xVals[count] = valX;
                            yVals[count] = valY;
                            count++;
                        }

                        break;
                    case 2:
                        if (!isCal2) {

                            state = 0;
                            xVals = new int[15];
                            yVals = new int[15];
                            count = 0;
                        } else if (count == 15) {

                            finished2 = true;
                            x2 = (int) getAverage(xVals);
                            y2 = (int) getAverage(yVals);
                            xVals = new int[15];
                            yVals = new int[15];
                            count = 0;
                        } else {
                            xVals[count] = valX;
                            yVals[count] = valY;
                            count++;
                        }
                        break;
                    case 3:
                        if (!isCal3) {

                            state = 0;
                            xVals = new int[15];
                            yVals = new int[15];
                            count = 0;
                        } else if (count == 15) {

                            finished3 = true;
                            x3 = (int) getAverage(xVals);
                            y3 = (int) getAverage(yVals);
                            xVals = new int[15];
                            yVals = new int[15];
                            count = 0;
                        } else {
                            xVals[count] = valX;
                            yVals[count] = valY;
                            count++;
                        }
                        break;
                    case 4:
                        CalibrateTouch ct = new CalibrateTouch();
                        double[] rt = ct.getMatrix(x1, y1, x2, y2, x3, y3, 400, 100, 100, 550, 700, 550);
                        a = rt[0];
                        b = rt[1];
                        c = rt[2];
                        d = rt[3];
                        e = rt[4];
                        f = rt[5];
                        isCalibrating = false;
                        saveImage = new BufferedImage(drawingPanel.getWidth(), drawingPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
                        break;
                }


                if (finished1) {
                    g.setColor(Color.GREEN);
                } else if (isCal1) {
                    g.setColor(Color.RED);
                } else {
                    g.setColor(Color.BLACK);
                }
                g.fillOval(400, 200, 10, 10);

                if (finished2) {
                    g.setColor(Color.GREEN);
                } else if (isCal2) {
                    g.setColor(Color.RED);
                } else {
                    g.setColor(Color.BLACK);
                }
                g.fillOval(100, 550, 10, 10);

                if (finished3) {
                    g.setColor(Color.GREEN);
                } else if (isCal3) {
                    g.setColor(Color.RED);
                } else {
                    g.setColor(Color.BLACK);
                }
                g.fillOval(700, 550, 10, 10);

                drawing.setImage(saveImage);
                drawLabel.setIcon(drawing);
                drawingPanel.repaint();

            } else if (!clearSet) {

                int xx = srcLength - getX1();
                int yy = getY1() - drawLabel.getLocationOnScreen().y;

                xx = (int) (a * xx + b * yy + c);
                yy = (int) (d * xx + e * yy + f);
                //yy=yy-100;
                //System.out.println("Label-"+drawLabel.getX()+","+drawLabel.getY()+"\n JPanel="+drawingPanel.getX()+","+drawingPanel.getY());
                //System.out.println("Label-"+drawLabel.getLocationOnScreen().x+","+drawLabel.getLocationOnScreen().y+"\n JPanel="+drawingPanel.getLocationOnScreen().x+","+drawingPanel.getLocationOnScreen().y);
                if ((prevX == 0 && prevY == 0) || (xx == 0 && yy == 0)) {
                    try {
                        Robot rb = new Robot();
                    } catch (AWTException ex) {
                        Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    g.setColor(linecolor);
                    g.setStroke(new BasicStroke(jSlider1.getValue()));
                    //System.out.println("prevX= " + prevX + " prevY= " + prevY + " x= " + xx + " y= " + yy);
                    //  System.out.println("x= " + xx + " y= " + yy);
                    //     if (xx < 800) {
                    g.fillOval(xx, yy, 10, 10);
                    //g.drawLine(prevX, prevY, xx, yy);
                    drawing.setImage(saveImage);
                    drawLabel.setIcon(drawing);
                    drawingPanel.repaint();
                    //     } else {
                        /*
                    rb.mouseMove(xx, yy);
                    System.out.println("mouse moving");*/
                    //    }

                }
                prevX = xx;
                prevY = yy;

            } else {
                saveImage = imageDb[saveImageNum];
                clearSet = false;

            }
        }
    };

    private double getAverage(int[] ab) {

        Arrays.sort(ab);
        double fg = 0;

        for (int h = 6; h <= 10; h++) {
            fg = fg + ab[h];
        }

        return fg / 5;
    }

    public void showMessage(){
        MessageFrame ms = new MessageFrame("Wait till the cameras are loaded");
        ms.setVisible(deviceStarted&&!isImageSet);
    }
}
